package com.sevenre.trackre.driver.network;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.sevenre.trackre.driver.datatypes.Stop;
import com.sevenre.trackre.driver.datatypes.TaggingStop;
import com.sevenre.trackre.driver.datatypes.Trip;
import com.sevenre.trackre.driver.utils.Constants;

public class Server {

	final static String base_url = "http://trackre.net/school/ws-01/driver-api/dev/1.0/";
	final static String authenticate = base_url + "authenticate/vehicle?";
	final static String vehicle = base_url + "vehicle/";
	final static String drop = vehicle + "gettrips?tripType=drop&";
	final static String pickUp = vehicle + "gettrips?tripType=pickUp&";
	
	//variables in json
	final static String SUCCESS = "success", VEHICLE_ID = "vehicleId", DATA = "data", TRIPS = "trips",
			ROUTE_ID = "routeId", ROUTE_DISPLAY_NAME = "routeNameDispval", TRIP_ID = "tripId",
			TRIP_TYPE = "tripType", TRIP_NAME_DISP = "tripNameDispval", TRIP_STATUS = "tripStatus",
			TIME = "time";
	
	public static String getVehicleId(String vehicleNo) {
		String url = authenticate + "vehicleNo=" + vehicleNo + "&schoolId=" + Constants.SCHOOL_ID;
		String result = readFromUrl(url);
		JSONObject object;
		System.out.println(result);
		try {
			object = new JSONObject(result);
			if(object.has(SUCCESS) && object.getBoolean(SUCCESS)) {
				JSONObject data = object.getJSONObject(DATA);
				return data.getString(VEHICLE_ID);
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static ArrayList<String> getPickUpStop() {
		return null;
	}

	public static ArrayList<Trip> getPickUpTrip() {
		String url = drop + VEHICLE_ID +"="+ Constants.VEHICLE_ID;
		String result = readFromUrl(url);
		System.out.println(url);
		System.out.println(result);
		JSONObject object;
		ArrayList<Trip> tirpList = new ArrayList<Trip>();
		try {
			object = new JSONObject(result);
			if(object.has(SUCCESS) && object.getBoolean(SUCCESS)) {
				JSONArray trips = (object.getJSONObject(DATA)).getJSONArray(TRIPS);
				System.out.println("lenght : " + trips.length());
				for (int i = 0; i < trips.length(); i++) {
					 JSONObject o = trips.getJSONObject(i);
					 String name = o.getString(ROUTE_DISPLAY_NAME);
					 String time = o.getString(TIME);
					 String title = o.getString(TRIP_NAME_DISP);
					 String tag = o.getString(TRIP_STATUS);
					 boolean isTagged = true;
					 if ("tag".equals(tag)) {
						 isTagged = false;
					 }
					 tirpList.add(new Trip(name, time, title, "DROP",isTagged));
					 System.out.println(name);
					 System.out.println(time);
					 System.out.println(title);
				}
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
		
		return tirpList ;
	}

	public static ArrayList<Trip> getDropTrip() {
		ArrayList<Trip> tirpList = new ArrayList<Trip>();
		return tirpList ;
	}

	public static ArrayList<Stop> getStopList() {

		ArrayList<Stop> stopList = new ArrayList<Stop>();
		stopList.add(new Stop("Munich", "06:13 AM", "01"));
		stopList.add(new Stop("Munich", "06:23 AM", "02"));
		stopList.add(new Stop("Munich", "06:33 AM", "03"));
		stopList.add(new Stop("Munich", "06:43 AM", "04"));
		stopList.add(new Stop("Munich", "06:53 AM", "05"));
		stopList.add(new Stop("Munich", "06:63 PM", "06"));
		stopList.add(new Stop("Munich", "07:03 PM", "07"));
		stopList.add(new Stop("Munich", "07:13 PM", "08"));
		stopList.add(new Stop("Munich", "07:23 PM", "09"));
		stopList.add(new Stop("Munich", "07:33 PM", "10"));
		
		return stopList ;
	}

	public static ArrayList<TaggingStop> getTaggingStopList() {

		ArrayList<TaggingStop> stopList = new ArrayList<TaggingStop>();
		stopList.add(new TaggingStop("Munich", "06:13 AM", "01"));
		stopList.add(new TaggingStop("Munich", "06:23 AM", "02"));
		stopList.add(new TaggingStop("Munich", "06:33 AM", "03"));
		stopList.add(new TaggingStop("Munich", "06:43 AM", "04"));
		stopList.add(new TaggingStop("Munich", "06:53 AM", "05"));
		stopList.add(new TaggingStop("Munich", "06:63 PM", "06"));
		stopList.add(new TaggingStop("Munich", "07:03 PM", "07"));
		stopList.add(new TaggingStop("Munich", "07:13 PM", "08"));
		stopList.add(new TaggingStop("Munich", "07:23 PM", "09"));
		stopList.add(new TaggingStop("Munich", "07:33 PM", "10"));
		
		return stopList ;
	}

	
	public static String getDate() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date(System.currentTimeMillis());
        return sdf.format(date);
    }

	public static void addCurrentStatus(String date, String status) {
		// TODO Auto-generated method stub
		
	}

	public static void addPosition(String lat, String lng, String speed,
			String time) {
		// TODO Auto-generated method stub
		
	}
	
	public static String readFromUrl(String urlString) {
		BufferedReader in;
		try {
			in = new BufferedReader(new InputStreamReader(
					new URL(urlString).openStream()));
			String inputLine;
			StringBuilder builder = new StringBuilder();
			while ((inputLine = in.readLine()) != null)
				builder.append(inputLine);
			in.close();
			return builder.toString();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return "";
	}
}
